<!-- 会签管理 > 待发送会签单位列表 -->
<template>
  <view>
    <!-- <van-contact-card type="add" @click="onAdd" add-text="添加更多会签单位"/> -->
    <van-cell title="添加更多会签单位" icon="plus" @click="onAdd" is-link/>
    <template v-if="unConSignList.length>0">
      <van-field v-model="sendHuiqianObj.limitDays" type="number" label="限时办理天数" placeholder="请输入" input-align="right"/>
    </template>
    <van-list v-model="loading" :finished="finished">
      <van-checkbox-group v-model="result" @change="onChange">
        <van-cell-group>
          <van-cell v-for="(item, index) in unConSignList" clickable :key="index" :title="item.countersignDept"
                    @click="toggle(index)">
            <template #label>
              <view class="padding-xs">
                <text class="text-gray text-sm margin-right-sm">接收人:{{ item.receiverName }}</text>
              </view>
            </template>
            <template #right-icon>
              <van-checkbox :name="item" ref="checkboxes"/>
            </template>
          </van-cell>
        </van-cell-group>
      </van-checkbox-group>
    </van-list>
    <view class="padding-top-lg padding-bottom-lg" v-if="unConSignList.length>0">
      <van-row type="flex" justify="space-around">
        <van-col span="10">
          <van-button :disabled="isDisabled" type="info" round plain hairline block @click="toDelete">删除</van-button>
        </van-col>
        <van-col span="10">
          <van-button :disabled="isDisabled" type="info" round block @click="toSend">发送会签</van-button>
        </van-col>
      </van-row>
    </view>
  </view>
</template>

<script>
export default {
  props: {
    paramObject: {
      proInstId: String,
      fwId: String,
      param01: Object,//获取签批功能菜单所需要的参数
      param02: Object
    },

    unConSignList: Array
  },

  data() {
    return {
      result: [],
      loading: false,
      finished: true, //不显示加载中
      isDisabled: true,
      sendHuiqianObj: {
        receiverIdArr: [],
        limitDays: '',//限时办理天数
      }
    }
  },
  watch: {
    //限时天数大于0
    sendHuiqianObj: {
      handler(nVal, oVal) {
        if (this.util.isNotEmpty(nVal.limitDays)) {
          if (nVal.limitDays == 0) {
            this.sendHuiqianObj.limitDays = ''
          } else if (nVal.limitDays < 0) {
            this.sendHuiqianObj.limitDays = Math.abs(nVal.limitDays)
          }
        }
      },
      immediate: true,
      deep: true
    }
  },
  mounted() {

  },
  methods: {

    toggle(index) {
      this.$refs.checkboxes[index].toggle();
    },

    onChange(item) {
      if (this.result.length > 0) {
        this.isDisabled = false
      } else {
        this.isDisabled = true
      }
    },

    onAdd() {
      if (this.paramObject.fwId != undefined) {
        uni.navigateTo({
          url: "/pages/admin/sendMsg/add-org?fwId=" + this.paramObject.fwId + "&proInstId=" + this.paramObject.proInstId
        })
      } else {
        this.$toast('缺少参数：fwid')
      }
    },

    toDelete() {
      this.$dialog.confirm({
        title: '系统提示',
        message: '是否确认删除所选单位?',
      }).then(() => {
        let uuidArr = []
        this.result.forEach((item) => {
          uuidArr.push(item.uuid)
        })
        this.$emit('deleteConSign', {uuids: uuidArr})
        this.isDisabled = true //重置按钮为不可点击
        this.result = []
      }).catch(() => {
        // on cancel
      });
    },
    toSend() {
      this.$dialog.confirm({
        confirmButtonColor: '#1989fa',
        title: '系统提示',
        message: '是否确认发送会签?',
      }).then(() => {//确定按钮
        //将选中单位传递给父组件进行会签提交
        this.result.forEach((item) => {
          this.sendHuiqianObj.receiverIdArr.push(item.receiverId)
        })
        this.$emit('sendHuiqian', this.sendHuiqianObj)//result选中的单位
        this.sendHuiqianObj.receiverIdArr=[]
        this.sendHuiqianObj.limitDays=''
      }).catch(() => {
        // on cancel
      });
    },


  },
}
</script>

<style scoped>
</style>
